<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>SkyWalking | Hexo</title>
  <meta name="keywords" content=" Spring Cloud Alibaba ">
  <meta name="description" content="SkyWalking | Hexo">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
<meta name="description" content="如何解决消息过期失效问题RabbtiMQ 是可以设置过期时间的，也就是 TTL。如果消息在 queue 中积压超过一定的时间就会被 RabbitMQ 给清理掉，这个数据就没了。这就是说数据不会大量积压在 mq 里，而是大量的数据会直接搞丢。解决方法就是采用批量重导。 批量重导，也就是后期的一种手动补偿手段。当高峰期过去之后将丢失的那部分数据，写个临时程序查出来，重新放到MQ里，把白天的数据补回来。">
<meta property="og:type" content="article">
<meta property="og:title" content="Rabbit MQ（面试）">
<meta property="og:url" content="https://xiaomaer-sss.gitee.io/xiaomaer-sss/2023/02/03/Rabbit%20MQ%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="如何解决消息过期失效问题RabbtiMQ 是可以设置过期时间的，也就是 TTL。如果消息在 queue 中积压超过一定的时间就会被 RabbitMQ 给清理掉，这个数据就没了。这就是说数据不会大量积压在 mq 里，而是大量的数据会直接搞丢。解决方法就是采用批量重导。 批量重导，也就是后期的一种手动补偿手段。当高峰期过去之后将丢失的那部分数据，写个临时程序查出来，重新放到MQ里，把白天的数据补回来。">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-02-03T13:04:21.063Z">
<meta property="article:modified_time" content="2023-02-04T03:42:41.460Z">
<meta property="article:author" content="kiwi">
<meta property="article:tag" content="队列">
<meta property="article:tag" content="异步">
<meta name="twitter:card" content="summary">


<link rel="icon" href="/img/avatar.jpg">

<link href="/css/style.css?v=1.1.0" rel="stylesheet">

<link href="/css/hl_theme/atom-light.css?v=1.1.0" rel="stylesheet">

<link href="//cdn.jsdelivr.net/npm/animate.css@4.1.0/animate.min.css" rel="stylesheet">

<script src="//cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
<script src="/js/titleTip.js?v=1.1.0" ></script>

<script src="//cdn.jsdelivr.net/npm/highlightjs@9.16.2/highlight.pack.min.js"></script>
<script>
    hljs.initHighlightingOnLoad();
</script>

<script src="//cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js"></script>



<script src="//cdn.jsdelivr.net/npm/jquery.cookie@1.4.1/jquery.cookie.min.js" ></script>

<script src="/js/iconfont.js?v=1.1.0" ></script>

<meta name="generator" content="Hexo 5.4.2"></head>
<div style="display: none">
  <input class="theme_disqus_on" value="false">
  <input class="theme_preload_comment" value="">
  <input class="theme_blog_path" value="">
  <input id="theme_shortcut" value="true" />
  <input id="theme_highlight_on" value="true" />
  <input id="theme_code_copy" value="true" />
</div>



<body>
<aside class="nav">
    <div class="nav-left">
        <a href="/"
   class="avatar_target">
    <img class="avatar"
         src="/img/avatar.jpg"/>
</a>
<div class="author">
    <span>kiwi</span>
</div>

<div class="icon">
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
    
        
            <a title="csdn"
               href="https://blog.csdn.net/weixin_45340300?type=blog"
               target="_blank">
                
                    <svg class="iconfont-svg" aria-hidden="true">
                        <use xlink:href="#icon-csdn"></use>
                    </svg>
                
            </a>
        
    
        
    
        
    
        
            <a title="email"
               href="mailto:jackma.h@foxmail.com"
               target="_blank">
                
                    <svg class="iconfont-svg" aria-hidden="true">
                        <use xlink:href="#icon-email"></use>
                    </svg>
                
            </a>
        
    
        
            <a title="qq"
               href="http://wpa.qq.com/msgrd?v=3&uin=761791958&site=qq&menu=yes"
               target="_blank">
                
                    <svg class="iconfont-svg" aria-hidden="true">
                        <use xlink:href="#icon-qq"></use>
                    </svg>
                
            </a>
        
    
        
    
        
    
</div>




<ul>
    <li>
        <div class="all active" data-rel="All">All
            
                <small>(34)</small>
            
        </div>
    </li>
    
        
            
                <li>
                    <div data-rel="开发">
                        <i class="fold iconfont icon-right"></i>
                        
                        开发
                        <small>(34)</small>
                        
                    </div>
                    
                        <ul class="sub hide">
                            
                                <li>
                                    <div data-rel="开发<--->框架">
                                        
                                        框架
                                        
                                            <small>(14
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="开发<--->其他">
                                        
                                        其他
                                        
                                            <small>(5
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="开发<--->数据库">
                                        
                                        数据库
                                        
                                            <small>(2
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="开发<--->疑难杂症">
                                        
                                        疑难杂症
                                        
                                            <small>(2
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="开发<--->中间件">
                                        
                                        中间件
                                        
                                            <small>(5
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                                <li>
                                    <div data-rel="开发<--->Java">
                                        
                                        Java
                                        
                                            <small>(6
                                                )</small>
                                        
                                    </div>
                                    
                                </li>
                            
                        </ul>
                    
                </li>
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
        
            
        
    
</ul>
<div class="left-bottom">
    <div class="menus">
        
            
            
            
    </div>
    <div>
        
        
    </div>
</div>
<input type="hidden" id="yelog_site_posts_number" value="34">
<input type="hidden" id="yelog_site_word_count" value="83.6k">
<div style="display: none">
    <span id="busuanzi_value_site_uv"></span>
    <span id="busuanzi_value_site_pv"></span>
</div>

    </div>
    <div class="nav-right">
        <div class="friends-area">
    <div class="friends-title">
        Links
        <i class="iconfont icon-left"></i>
    </div>
    <div class="friends-content">
        <ul>
            
            <li><a target="_blank" href="https://xiaomaer-sss.gitee.io/">叶落阁</a></li>
            
        </ul>
    </div>
</div>
        <div class="title-list">
    <div class="right-top">
        <div id="default-panel">
            <i class="iconfont icon-search" data-title="搜索 快捷键 i"></i>
            <div class="right-title">All</div>
            <i class="iconfont icon-file-tree" data-title="切换到大纲视图 快捷键 w"></i>
        </div>
        <div id="search-panel">
            <i class="iconfont icon-left" data-title="返回"></i>
            <input id="local-search-input" autocomplete="off"/>
            <label class="border-line" for="input"></label>
            <i class="iconfont icon-case-sensitive" data-title="大小写敏感"></i>
            <i class="iconfont icon-tag" data-title="标签"></i>
        </div>
        <div id="outline-panel" style="display: none">
            <div class="right-title">大纲</div>
            <i class="iconfont icon-list" data-title="切换到文章列表"></i>
        </div>
    </div>

    <div class="tags-list">
    <input id="tag-search" />
    <div class="tag-wrapper">
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>队列</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>分布式</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>缓存</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>流式处理</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>区块链</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>实战面试题</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>数据库</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>异步</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Bug</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Docker</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Java</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Java-集合</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Java-进阶</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Java-JVM</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Java基础</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>LB</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>MQ</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Mybatis</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>MySQL</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Redis</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Spring</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>Spring Cloud Alibaba</a>
            </li>
        
            <li class="article-tag-list-item">
                <i class="iconfont icon-tag"></i><a>SpringBoot</a>
            </li>
        
    </div>

</div>

    
    <nav id="title-list-nav">
        
        
        <a  class="All 开发 中间件 "
           href="/xiaomaer-sss/2023/02/03/Rabbit%20MQ%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="队列,异步"
           data-author="" >
            <span class="post-title" title="Rabbit MQ（面试）">Rabbit MQ（面试）</span>
            <span class="post-date" title="2023-02-03 21:04:21">2023/02/03</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/02/03/%E5%AE%9E%E6%88%98%E9%9D%A2%E8%AF%95%E9%A2%98%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="实战面试题"
           data-author="" >
            <span class="post-title" title="实战面试题">实战面试题</span>
            <span class="post-date" title="2023-02-03 17:11:07">2023/02/03</span>
        </a>
        
        
        <a  class="All 开发 中间件 "
           href="/xiaomaer-sss/2023/02/02/Kafka%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="分布式,流式处理,MQ"
           data-author="" >
            <span class="post-title" title="Kafka（面试）">Kafka（面试）</span>
            <span class="post-date" title="2023-02-02 09:51:06">2023/02/02</span>
        </a>
        
        
        <a  class="All 开发 中间件 "
           href="/xiaomaer-sss/2023/02/02/Zookeeper%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="分布式"
           data-author="" >
            <span class="post-title" title="Zookeeper（面试）">Zookeeper（面试）</span>
            <span class="post-date" title="2023-02-02 09:50:33">2023/02/02</span>
        </a>
        
        
        <a  class="All 开发 中间件 "
           href="/xiaomaer-sss/2023/02/01/Redis%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Redis,缓存"
           data-author="" >
            <span class="post-title" title="Redis（面试）">Redis（面试）</span>
            <span class="post-date" title="2023-02-01 14:09:09">2023/02/01</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/30/SpringBoot%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="SpringBoot"
           data-author="" >
            <span class="post-title" title="SpringBoot（面试）">SpringBoot（面试）</span>
            <span class="post-date" title="2023-01-30 20:26:42">2023/01/30</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/30/Mybatis%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Mybatis"
           data-author="" >
            <span class="post-title" title="Mybatis（面试）">Mybatis（面试）</span>
            <span class="post-date" title="2023-01-30 19:13:44">2023/01/30</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/30/Spring%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Spring"
           data-author="" >
            <span class="post-title" title="Spring（面试）">Spring（面试）</span>
            <span class="post-date" title="2023-01-30 19:12:30">2023/01/30</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/01/29/Java-JVM%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Java-JVM"
           data-author="" >
            <span class="post-title" title="Java-JVM（面试）">Java-JVM（面试）</span>
            <span class="post-date" title="2023-01-29 16:31:34">2023/01/29</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/01/29/Java-%E8%BF%9B%E9%98%B6%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Java-进阶"
           data-author="" >
            <span class="post-title" title="Java-进阶（面试）">Java-进阶（面试）</span>
            <span class="post-date" title="2023-01-29 13:11:10">2023/01/29</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/01/29/Java-%E9%9B%86%E5%90%88%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Java-集合"
           data-author="" >
            <span class="post-title" title="Java-集合（面试）">Java-集合（面试）</span>
            <span class="post-date" title="2023-01-29 09:18:51">2023/01/29</span>
        </a>
        
        
        <a  class="All 开发 数据库 "
           href="/xiaomaer-sss/2023/01/28/%E6%95%B0%E6%8D%AE%E5%BA%93-MySQL%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="MySQL"
           data-author="" >
            <span class="post-title" title="数据库-MySQL（面试）">数据库-MySQL（面试）</span>
            <span class="post-date" title="2023-01-28 16:50:59">2023/01/28</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/01/27/Java-%E5%9F%BA%E7%A1%80%EF%BC%88%E9%9D%A2%E8%AF%95%EF%BC%89/"
           data-tag="Java基础"
           data-author="" >
            <span class="post-title" title="Java-基础（面试）">Java-基础（面试）</span>
            <span class="post-date" title="2023-01-27 19:42:58">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 其他 "
           href="/xiaomaer-sss/2023/01/27/%E5%8C%BA%E5%9D%97%E9%93%BE%E9%92%B1%E5%8C%85/"
           data-tag="区块链"
           data-author="" >
            <span class="post-title" title="区块链钱包">区块链钱包</span>
            <span class="post-date" title="2023-01-27 18:57:51">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 其他 "
           href="/xiaomaer-sss/2023/01/27/Truffle%E5%B8%B8%E7%94%A8%E5%91%BD%E4%BB%A4/"
           data-tag="区块链"
           data-author="" >
            <span class="post-title" title="Truffle常用命令&amp;Web3常用方法">Truffle常用命令&amp;Web3常用方法</span>
            <span class="post-date" title="2023-01-27 18:57:51">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 其他 "
           href="/xiaomaer-sss/2023/01/27/Truffle&OpenZeppelin%E7%8E%AF%E5%A2%83%E6%90%AD%E5%BB%BA/"
           data-tag="区块链"
           data-author="" >
            <span class="post-title" title="Truffle&amp;OpenZeppelin环境搭建">Truffle&amp;OpenZeppelin环境搭建</span>
            <span class="post-date" title="2023-01-27 18:57:51">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 其他 "
           href="/xiaomaer-sss/2023/01/27/Solidity/"
           data-tag="区块链"
           data-author="" >
            <span class="post-title" title="Solidity">Solidity</span>
            <span class="post-date" title="2023-01-27 18:57:51">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 中间件 "
           href="/xiaomaer-sss/2023/01/27/Nginx/"
           data-tag="LB"
           data-author="" >
            <span class="post-title" title="Nginx">Nginx</span>
            <span class="post-date" title="2023-01-27 18:57:51">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/27/DockerFile/"
           data-tag="Docker"
           data-author="" >
            <span class="post-title" title="Dockerfile">Dockerfile</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 其他 "
           href="/xiaomaer-sss/2023/01/27/%E5%8C%BA%E5%9D%97%E9%93%BE%E4%B9%8B%E4%BB%A5%E5%A4%AA%E5%9D%8A/"
           data-tag="区块链"
           data-author="" >
            <span class="post-title" title="区块链之以太坊">区块链之以太坊</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 数据库 "
           href="/xiaomaer-sss/2023/01/27/%E5%B8%B8%E7%94%A8SQL%E8%AF%AD%E6%B3%95%E6%95%B4%E7%90%86/"
           data-tag="MySQL,数据库"
           data-author="" >
            <span class="post-title" title="常用SQL语法整理">常用SQL语法整理</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/27/docker%E8%BF%9B%E9%98%B6/"
           data-tag="Docker"
           data-author="" >
            <span class="post-title" title="Docker进阶">Docker进阶</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/27/Docker%E5%9F%BA%E7%A1%80/"
           data-tag="Docker"
           data-author="" >
            <span class="post-title" title="Docker基础">Docker基础</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/27/Docker%E5%AE%89%E8%A3%85%E5%B8%B8%E8%A7%81%E7%BB%84%E4%BB%B6/"
           data-tag="Docker"
           data-author="" >
            <span class="post-title" title="Docker安装常见组件">Docker安装常见组件</span>
            <span class="post-date" title="2023-01-27 17:45:23">2023/01/27</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/SkyWalking/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="SkyWalking">SkyWalking</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/Sentinel/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="Sentinel">Sentinel</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/Seata/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="Seata">Seata</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/Ribbon/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="Ribbon">Ribbon</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 疑难杂症 "
           href="/xiaomaer-sss/2023/01/26/github%E6%8B%89%E5%8F%96%E4%BB%A3%E7%A0%81%E6%8A%A5%20git@github.com's%20password/"
           data-tag="Bug"
           data-author="" >
            <span class="post-title" title="github拉取代码报 &#34;git@github.com&#39;s password&#34;">github拉取代码报 &#34;git@github.com&#39;s password&#34;</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 疑难杂症 "
           href="/xiaomaer-sss/2023/01/26/gitee%E6%90%AD%E5%BB%BA%E4%B8%AA%E4%BA%BA%E5%8D%9A%E5%AE%A2%E6%95%99%E7%A8%8B/"
           data-tag="Bug"
           data-author="" >
            <span class="post-title" title="gitee搭建个人博客教程">gitee搭建个人博客教程</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/GateWay/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="GateWay">GateWay</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/26/Fegin/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="Fegin">Fegin</span>
            <span class="post-date" title="2023-01-26 21:39:44">2023/01/26</span>
        </a>
        
        
        <a  class="All 开发 框架 "
           href="/xiaomaer-sss/2023/01/20/Nacos/"
           data-tag="Spring Cloud Alibaba"
           data-author="" >
            <span class="post-title" title="Nacos">Nacos</span>
            <span class="post-date" title="2023-01-20 17:12:00">2023/01/20</span>
        </a>
        
        
        <a  class="All 开发 Java "
           href="/xiaomaer-sss/2023/01/17/hello-world/"
           data-tag="Java"
           data-author="" >
            <span class="post-title" title="Hello World">Hello World</span>
            <span class="post-date" title="2023-01-17 15:40:39">2023/01/17</span>
        </a>
        
        <div id="no-item-tips">

        </div>
    </nav>
    <div id="outline-list">
    </div>
</div>

    </div>
    <div class="hide-list">
        <div class="semicircle" data-title="切换全屏 快捷键 s">
            <div class="brackets first"><</div>
            <div class="brackets">&gt;</div>
        </div>
    </div>
</aside>
<div id="post">
    <div class="pjax">
        <article id="post-SkyWalking" class="article article-type-post" itemscope itemprop="blogPost">
    
        <h1 class="article-title">SkyWalking</h1>
    
    <div class="article-meta">
        
        
        
        <span class="book">
            <i class="iconfont icon-category"></i>
            
            
            <a  data-rel="开发">开发</a> > 
            
            <a  data-rel="开发&lt;---&gt;框架">框架</a>
            
        </span>
        
        
        <span class="tag">
            <i class="iconfont icon-tag"></i>
            
            <a class="color1">Spring Cloud Alibaba</a>
            
        </span>
        
    </div>
    <div class="article-meta">
        
            Created At : <time class="date" title='Updated At: 2023-01-26 22:10:18'>2023-01-26 21:39</time>
        
    </div>
    <div class="article-meta">
        
        <span>Count:3.8k</span>
        
        
        <span id="busuanzi_container_page_pv">
            Views 👀 :<span id="busuanzi_value_page_pv">
                <span class="count-comment">
                    <span class="spinner">
                      <div class="cube1"></div>
                      <div class="cube2"></div>
                    </span>
                </span>
            </span>
        </span>
        
        
    </div>
    
    <div class="toc-ref">
    
        <ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#SkyWalking%E7%8E%AF%E5%A2%83%E9%83%A8%E7%BD%B2%E6%90%AD%E5%BB%BA"><span class="toc-text">SkyWalking环境部署搭建</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#%E6%90%AD%E5%BB%BASkyWalking-OAP%E6%9C%8D%E5%8A%A1"><span class="toc-text">搭建SkyWalking OAP服务</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BD%E8%B5%84%E6%BA%90"><span class="toc-text">下载资源</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%90%AF%E5%8A%A8%E6%9C%8D%E5%8A%A1"><span class="toc-text">启动服务</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E8%AE%BF%E9%97%AEUI"><span class="toc-text">访问UI</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SkyWalking%E6%8E%A5%E5%85%A5%E5%BE%AE%E6%9C%8D%E5%8A%A1"><span class="toc-text">SkyWalking接入微服务</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%B8%8B%E8%BD%BDJava-Agent"><span class="toc-text">下载Java Agent</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#win%E4%B8%8B%E6%95%B4%E5%90%88SkyWalking"><span class="toc-text">win下整合SkyWalking</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Linux%E4%B8%8B%E6%95%B4%E5%90%88SkyWalking"><span class="toc-text">Linux下整合SkyWalking</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SkyWalking%E6%8C%81%E4%B9%85%E5%8C%96%E5%88%B0MySQL"><span class="toc-text">SkyWalking持久化到MySQL</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E8%87%AA%E5%AE%9A%E4%B9%89%E9%93%BE%E8%B7%AF%E8%BF%BD%E8%B8%AA"><span class="toc-text">自定义链路追踪</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E9%9B%86%E6%88%90%E6%97%A5%E5%BF%97"><span class="toc-text">集成日志</span></a></li><li class="toc-item toc-level-3"><a class="toc-link" href="#%E5%91%8A%E8%AD%A6"><span class="toc-text">告警</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E5%91%8A%E8%AD%A6%E8%A7%84%E5%88%99"><span class="toc-text">告警规则</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#Webhook"><span class="toc-text">Webhook</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%91%8A%E8%AD%A6%E5%AE%9E%E7%8E%B0%E5%8F%91%E9%80%81%E9%82%AE%E4%BB%B6"><span class="toc-text">告警实现发送邮件</span></a></li></ol></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#SkyWalking%E9%9B%86%E7%BE%A4"><span class="toc-text">SkyWalking集群</span></a></li></ol></li></ol>
    
<style>
    .left-col .switch-btn,
    .left-col .switch-area {
        display: none;
    }
    .toc-level-3 i,
    .toc-level-3 ol {
        display: none !important;
    }
</style>
</div>
    
    <div class="article-entry" itemprop="articleBody">
      
        <p>SkyWalking是一个国产开源框架，2015年由吴晟开源 ， 2017年加入Apache孵化器。 <strong>SkyWalking是分布式系统的应用程序性能监视工具</strong>，专为微服务、云原生架构和基于容器（Docker、K8s、Mesos）架构而设计。它是一款优秀的APM（Application Performance Management）工具，包括了分布式追踪、性能指标分析、应用和服务依赖分析等。 </p>
<p>官网地址： <a target="_blank" rel="noopener" href="https://skywalking.apache.org/">https://skywalking.apache.org/</a></p>
<p>下载地址：<a target="_blank" rel="noopener" href="http://skywalking.apache.org/downloads/">http://skywalking.apache.org/downloads/</a> </p>
<p>Github地址：<a target="_blank" rel="noopener" href="https://github.com/apache/skywalking">https://github.com/apache/skywalking</a></p>
<p>文档地址： <a target="_blank" rel="noopener" href="https://skyapm.github.io/document-cn-translation-of-skywalking/">https://skyapm.github.io/document-cn-translation-of-skywalking/</a> </p>
<p><strong>SkyWalking优点：</strong></p>
<ul>
<li>接入段无代码入侵（字节码增强）</li>
<li>支持java/.net/Node.js/php/go等语言</li>
<li>调用链路可视化，聚合报表较丰富</li>
<li>探针（JavaAgent）对吞吐量影响较小</li>
</ul>
<p><strong>SkyWalking主要功能：</strong></p>
<ul>
<li>多种监控手段，可以通过语言探针（JavaAgent）和service mesh获得监控的数据</li>
<li>支持多种语言自动探针，包括 Java，.NET Core 和 Node.JS</li>
<li>轻量高效，无需大数据平台和大量的服务器资源</li>
<li>模块化，UI、存储、集群管理都有多种机制可选</li>
<li>支持告警</li>
<li>优秀的可视化解决方案</li>
</ul>
<h2 id="SkyWalking环境部署搭建"><a href="#SkyWalking环境部署搭建" class="headerlink" title="SkyWalking环境部署搭建"></a>SkyWalking环境部署搭建</h2><p>SkyWalking组成部分如下，其中skywalking oapservice集群和SkyWalking webapp UI服务在一个资源中，部署起来也是相对简单。</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673320011194.png" alt="1673320011194"></p>
<ul>
<li>skywalking agent和业务系统绑定在一起，负责收集各种监控数据</li>
<li>Skywalking oapservice是负责处理监控数据的，比如接受skywalking agent的监控数据，并存储在数据库中。接受skywalking webapp的前端请求，从数据库查询数据，并返回数据给前端。Skywalking oapservice通常以集群的形式存在</li>
<li>skywalking webapp，前端界面，用于展示数据</li>
<li>数据库用于存储监控数据的数据库，比如mysql、elasticsearch等</li>
</ul>
<h3 id="搭建SkyWalking-OAP服务"><a href="#搭建SkyWalking-OAP服务" class="headerlink" title="搭建SkyWalking OAP服务"></a>搭建SkyWalking OAP服务</h3><h4 id="下载资源"><a href="#下载资源" class="headerlink" title="下载资源"></a>下载资源</h4><p>下载地址：<a target="_blank" rel="noopener" href="http://skywalking.apache.org/downloads/">http://skywalking.apache.org/downloads/</a> </p>
<p>选择合适的版本下载解压即可。（我这边使用的是v9.2.0）</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673321037268.png" alt="1673321037268"></p>
<h4 id="启动服务"><a href="#启动服务" class="headerlink" title="启动服务"></a>启动服务</h4><p>启动前建议修改SkyWalking-web-UI的默认端口8080（8080经常被其他程序占用），此处修改为8868端口。</p>
<p>修改路径为apache-skywalking-apm-bin\webapp\webapp.yml</p>
<p>启动apache-skywalking-apm-bin\bin\startup.bat（linux下启动startup.sh）</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673322353511.png" alt="1673322353511"></p>
<p>没有日志实时输出，在apache-skywalking-apm-bin\logs下查看启动日志。</p>
<p>启动后会后两个服务，一个是SkyWalking-oap-server，一个是SkyWalking-web-ui。SkyWalking-oap-server服务启动后会暴露11800和12800两个端口（端口可以在apache-skywalking-apm-bin\config\application.yml中修改），11800负责收集监控数据，12800负责接受前端请求。</p>
<h4 id="访问UI"><a href="#访问UI" class="headerlink" title="访问UI"></a>访问UI</h4><p>访问<a target="_blank" rel="noopener" href="http://127.0.0.1:8868/">http://127.0.0.1:8868/</a></p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673322705900.png" alt="1673322705900"></p>
<h3 id="SkyWalking接入微服务"><a href="#SkyWalking接入微服务" class="headerlink" title="SkyWalking接入微服务"></a>SkyWalking接入微服务</h3><h4 id="下载Java-Agent"><a href="#下载Java-Agent" class="headerlink" title="下载Java Agent"></a>下载Java Agent</h4><p>下载地址：<a target="_blank" rel="noopener" href="https://skywalking.apache.org/downloads/">https://skywalking.apache.org/downloads/</a></p>
<p>我这边选择的是最新版v8.13.0。下载完成后解压出来即可。</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673331206415.png" alt="1673331206415"></p>
<h4 id="win下整合SkyWalking"><a href="#win下整合SkyWalking" class="headerlink" title="win下整合SkyWalking"></a>win下整合SkyWalking</h4><p><strong>在需要整合SkyWalking的服务中添加参数</strong>，在IDEA中添加VM参数。linux下也是一样的使用java -jar带下面的参数运行。</p>
<pre><code>-javaagent:D:\develop\skywalking\skywalking-agent\skywalking-agent.jar
-DSW_AGENT_NAME=skywalking-demo
-DSW_AGENT_COLLECTOR_BACKEND_SERVICES=127.0.0.1:11800
</code></pre>
<p>参数说明：</p>
<p>-javaagent：skywalking-agent.jar在本地磁盘的路径，必须是本地</p>
<p>-DSW_AGENT_NAM：在SkyWalking上显示的服务名</p>
<p>-DSW_AGENT_COLLECTOR_BACKEND_SERVICES：SkyWalking的collector服务的IP和端口，负责收集监控数据的端口</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673331987563.png" alt="1673331987563"></p>
<p>启动项目，请求一个接口，查看控制台。</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673332150657.png" alt="1673332150657"></p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673332238320.png" alt="1673332238320"></p>
<p>整合了多个服务的效果（在需要整合的服务中添加对应的参数），调用链路就很清晰了。整合Gateway时需要注意添加plugin插件。</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673340421454.png" alt="1673340421454"></p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673340621354.png" alt="1673340621354"></p>
<h4 id="Linux下整合SkyWalking"><a href="#Linux下整合SkyWalking" class="headerlink" title="Linux下整合SkyWalking"></a>Linux下整合SkyWalking</h4><p>准备一个springboot程序，打成可执行的jar包，写一个shell脚本。在shell脚本上通过设置相关参数启动项目。</p>
<p>startup.sh</p>
<pre><code class="shell"> #!/bin/sh
 # SkyWalking Agent配置
export SW_AGENT_NAME=springboot‐skywalking‐demo #Agent名字,一般使用spring.application.name
export SW_AGENT_COLLECTOR_BACKEND_SERVICES=127.0.0.1:11800 #配置 Collector 地址。
export SW_AGENT_SPAN_LIMIT=2000 #配置链路的最大Span数量，默认为 300。
export JAVA_AGENT=‐javaagent:/usr/local/soft/apache‐skywalking‐apm‐bin/agent/skywalking‐agent.jar
java $JAVA_AGENT ‐jar springboot‐skywalking‐demo‐0.0.1‐SNAPSHOT.jar #jar启动
</code></pre>
<p>上面的脚本和在IDEA中启动写的vm参数是等价的。</p>
<h3 id="SkyWalking持久化到MySQL"><a href="#SkyWalking持久化到MySQL" class="headerlink" title="SkyWalking持久化到MySQL"></a>SkyWalking持久化到MySQL</h3><p>SkyWalking默认使用H2数据库存储，H2是一个基于内存的数据库，重启后数据就没有了。虽然SkyWalking监控数据是一个实时的，但运行时间长了内存容易满，就需要重启SkyWalking服务了。如果配置为持久化的数据库中（当然SkyWalking配合ES性能是最好），我们定期的进行数据清理即可，避免了频繁的重启服务。</p>
<ul>
<li><p><strong>修改配置</strong><br>修改apache-skywalking-apm-bin\config\application.yml文件</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673343266898.png" alt="1673343266898"></p>
</li>
<li><p><strong>添加mysql驱动</strong><br>向apache-skywalking-apm-bin\oap-libs目录下添加MySQL数据库对应的连接驱动包<br><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673343435341.png" alt="1673343435341"></p>
</li>
<li><p><strong>创建数据库</strong><br>在数据库中创建apache-skywalking-apm-bin\config\application.yml中的数据库。<br><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673343628202.png" alt="1673343628202"></p>
</li>
<li><p><strong>启动服务</strong></p>
<p>双击apache-skywalking-apm-bin\bin\startup.bat。<br>启动服务后swtest中就会建好很多的表。<br><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673343806168.png" alt="1673343806168"></p>
</li>
<li><p><strong>验证</strong><br>重启SkyWalking服务之后，之前的报错依然存在。<br><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673344218178.png" alt="1673344218178"></p>
</li>
</ul>
<h3 id="自定义链路追踪"><a href="#自定义链路追踪" class="headerlink" title="自定义链路追踪"></a>自定义链路追踪</h3><p>SkyWalking默认是会记录接口资源（也就是controller层）的链路，当我们希望对项目中的业务方法实现链路追踪，方便我们排查问题时，可以自定义链路追踪。</p>
<ul>
<li><p><strong>导入依赖</strong></p>
<pre><code class="pom">&lt;dependency&gt;
    &lt;groupId&gt;org.apache.skywalking&lt;/groupId&gt;
    &lt;artifactId&gt;apm-toolkit-trace&lt;/artifactId&gt;
    &lt;version&gt;8.5.0&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
</li>
<li><p><strong>添加追踪注解</strong><br>在需要加入追踪的业务方法上添加**@Trace**注解</p>
<pre><code class="java">@Trace
public List&lt;Order&gt; all() throws InterruptedException &#123;
    TimeUnit.SECONDS.sleep(2);
    return orderMapper.selectAll();
&#125;
</code></pre>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673355043313.png" alt="1673355043313"></p>
</li>
<li><p><strong>获取入参和返回值</strong><br>添加追踪业务方法**@Trace**注解的基础上，使用@Tags和@Tag获取入参和返回值</p>
<pre><code class="java">@Trace
@Tag(key=&quot;all&quot;,value=&quot;returnedObj&quot;)
public List&lt;Order&gt; all() throws InterruptedException &#123;
    TimeUnit.SECONDS.sleep(2);
    return orderMapper.selectAll();
&#125;

@Trace
@Tags(&#123;@Tag(key=&quot;get&quot;,value=&quot;returnedObj&quot;),// &quot;returnedObj&quot;为返回值的固定写法，返回值类型需要重写toString方法
       @Tag(key=&quot;get&quot;,value=&quot;arg[0]&quot;)&#125;) // &quot;arg[0]&quot; 代表第一个入参参数
public Order get(Integer id) &#123;
    return orderMapper.selectByPrimaryKey(id);
&#125;
</code></pre>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673355309545.png" alt="1673355309545"></p>
</li>
</ul>
<h3 id="集成日志"><a href="#集成日志" class="headerlink" title="集成日志"></a>集成日志</h3><p>logback官方配置地址：<a target="_blank" rel="noopener" href="https://github.com/apache/skywalking/blob/v8.5.0/docs/en/setup/service-agent/java-agent/Application-toolkit-logback-1.x.md">https://github.com/apache/skywalking/blob/v8.5.0/docs/en/setup/service-agent/java-agent/Application-toolkit-logback-1.x.md</a></p>
<ul>
<li><p>导入依赖</p>
<pre><code class="pom">&lt;dependency&gt;
    &lt;groupId&gt;org.apache.skywalking&lt;/groupId&gt;
    &lt;artifactId&gt;apm-toolkit-logback-1.x&lt;/artifactId&gt;
    &lt;version&gt;&#123;project.release.version&#125;&lt;/version&gt;
&lt;/dependency&gt;
</code></pre>
</li>
<li><p>添加配置文件<br>在项目resource下添加logback-spring.xml文件</p>
<pre><code class="xml">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;configuration&gt;
    &lt;!-- 引入 Spring Boot 默认的 logback XML 配置文件  --&gt;
    &lt;include resource=&quot;org/springframework/boot/logging/logback/defaults.xml&quot;/&gt;


    &lt;!--配置的是控制台日志输出，核心是获取TID追踪ID--&gt;
    &lt;appender name=&quot;console&quot; class=&quot;ch.qos.logback.core.ConsoleAppender&quot;&gt;
        &lt;!-- 日志的格式化 --&gt;
        &lt;encoder  class=&quot;ch.qos.logback.core.encoder.LayoutWrappingEncoder&quot;&gt;
            &lt;layout class=&quot;org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout&quot;&gt;
                &lt;Pattern&gt;-%clr(%d&#123;$&#123;LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS&#125;&#125;)&#123;faint&#125; %clr($&#123;LOG_LEVEL_PATTERN:-%5p&#125;) %clr($&#123;PID:- &#125;)&#123;magenta&#125; [%tid] %clr(---)&#123;faint&#125; %clr([%15.15t])&#123;faint&#125; %clr(%-40.40logger&#123;39&#125;)&#123;cyan&#125; %clr(:)&#123;faint&#125; %m%n$&#123;LOG_EXCEPTION_CONVERSION_WORD:-%wEx&#125;&#125;&lt;/Pattern&gt;
            &lt;/layout&gt;
        &lt;/encoder&gt;

    &lt;/appender&gt;

    &lt;!--配置的是SkyWalking中的日志，可以在SkyWalking UI中直接查看日志--&gt;
    &lt;appender name=&quot;grpc-log&quot; class=&quot;org.apache.skywalking.apm.toolkit.log.logback.v1.x.log.GRPCLogClientAppender&quot;&gt;
        &lt;encoder class=&quot;ch.qos.logback.core.encoder.LayoutWrappingEncoder&quot;&gt;
            &lt;layout class=&quot;org.apache.skywalking.apm.toolkit.log.logback.v1.x.mdc.TraceIdMDCPatternLogbackLayout&quot;&gt;
                &lt;Pattern&gt;%d&#123;yyyy-MM-dd HH:mm:ss.SSS&#125; [%X&#123;tid&#125;] [%thread] %-5level %logger&#123;36&#125; -%msg%n&lt;/Pattern&gt;
            &lt;/layout&gt;
        &lt;/encoder&gt;
    &lt;/appender&gt;

    &lt;!-- 设置 Appender --&gt;
    &lt;root level=&quot;INFO&quot;&gt;
        &lt;appender-ref ref=&quot;console&quot;/&gt; &lt;!--控制台输出日志--&gt;
        &lt;appender-ref ref=&quot;grpc-log&quot;/&gt; &lt;!--同步日志到SkyWalking UI中--&gt;
    &lt;/root&gt;

&lt;/configuration&gt;
</code></pre>
</li>
<li><p>修改配置文件<br>在skywalking-agent\config\agent.config文件最后添加以下配置</p>
<pre><code class="txt">#  指定要向其报告日志数据的grpc服务器的主机，也就是SkyWalking OAP服务主机地址
plugin.toolkit.log.grpc.reporter.server_host=$&#123;SW_GRPC_LOG_SERVER_HOST:127.0.0.1&#125;
#  SkyWalking OAP收集数据的端口，默认11800
plugin.toolkit.log.grpc.reporter.server_port=$&#123;SW_GRPC_LOG_SERVER_PORT:11800&#125;
#  日志信息最大size
plugin.toolkit.log.grpc.reporter.max_message_size=$&#123;SW_GRPC_LOG_MAX_MESSAGE_SIZE:10485760&#125;
#  数据上报超时时间，单位 S
plugin.toolkit.log.grpc.reporter.upstream_timeout=$&#123;SW_GRPC_LOG_GRPC_UPSTREAM_TIMEOUT:30&#125;
</code></pre>
</li>
<li><p>启动项目和SkyWalking服务<br>控制台输出TID，可以使用TID去SkyWalking UI中查询相关信息。</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673360305324.png" alt="1673360305324"></p>
<p>SkyWalking UI界面中可以直接查看日志信息。</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673360652653.png" alt="1673360652653"></p>
</li>
</ul>
<h3 id="告警"><a href="#告警" class="headerlink" title="告警"></a>告警</h3><p>SkyWalking告警功能是在6.x版本新增的，其核心由一组规则驱动，这些规则定义在apache-skywalking-apm-bin\config\alarm-settings.yml文件中。 告警规则的定义分为两部分： </p>
<ol>
<li><a target="_blank" rel="noopener" href="https://github.com/apache/skywalking/blob/v8.5.0/docs/en/setup/backend/backend-alarm.md">告警规则</a>：它们定义了应该如何触发度量警报，应该考虑什么条件</li>
<li><a target="_blank" rel="noopener" href="https://github.com/apache/skywalking/blob/v8.5.0/docs/en/setup/backend/backend-alarm.md">Webhook</a>（网络钩子）：定义当警告触发时，哪些服务终端需要被告知</li>
</ol>
<h4 id="告警规则"><a href="#告警规则" class="headerlink" title="告警规则"></a><strong>告警规则</strong></h4><p>SkyWalking的发行版都会默认提供apache-skywalking-apm-bin\alarm-settings.yml文件，里面预先定义了一些常用的告警规则。如下： </p>
<ol>
<li>过去 3 分钟内服务平均响应时间超过 1 秒</li>
<li>过去 2 分钟服务成功率低于80%</li>
<li>过去 3 分钟内服务响应时间超过 1s 的百分比</li>
<li>服务实例在过去 2 分钟内平均响应时间超过 1s，并且实例名称与正则表达式匹配</li>
<li>过去 2 分钟内端点平均响应时间超过 1 秒</li>
<li>过去 2 分钟内数据库访问平均响应时间超过 1 秒</li>
<li>过去 2 分钟内端点关系平均响应时间超过 1 秒</li>
</ol>
<p>这些预定义的告警规则，打开alarm-settings.yml文件即可看到 。</p>
<p><strong>告警规则配置项说明</strong>：</p>
<ul>
<li><strong>Rule name</strong>：规则名称，也是在告警信息中显示的唯一名称。必须以_rule结尾，前缀可自定义</li>
<li><strong>Metrics name</strong>：度量名称，取值为oal脚本中的度量名，目前只支持long、double和int类型。详见<a target="_blank" rel="noopener" href="https://github.com/apache/skywalking/blob/master/docs/en/guides/backend-oal-scripts.md">Official OAL script</a></li>
<li><strong>Include names</strong>：该规则作用于哪些实体名称，比如服务名，终端名（可选，默认为全部）</li>
<li><strong>Exclude names</strong>：该规则作不用于哪些实体名称，比如服务名，终端名（可选，默认为空）</li>
<li><strong>Threshold</strong>：阈值</li>
<li><strong>OP</strong>： 操作符，目前支持 &gt;、&lt;、=</li>
<li><strong>Period</strong>：多久告警规则需要被核实一下。这是一个时间窗口，与后端部署环境时间相匹配</li>
<li><strong>Count</strong>：在一个Period窗口中，如果values超过Threshold值（按op），达到Count值，需要发送警报</li>
<li><strong>Silence period</strong>：在时间N中触发报警后，在TN -&gt; TN + period这个阶段不告警。 默认情况下，它和Period一样，这意味着相同的告警（在同一个Metrics name拥有相同的Id）在同一个Period内只会触发一次</li>
<li><strong>message</strong>：告警消息 </li>
</ul>
<h4 id="Webhook"><a href="#Webhook" class="headerlink" title="Webhook"></a>Webhook</h4><p>Webhook可以简单理解为是一种Web层面的回调机制，通常由一些事件触发，与代码中的事件回调类似，只不过是Web层面的。由于是 Web层面的，所以当事件发生时，回调的不再是代码中的方法或函数，而是服务接口。</p>
<p>例如，在告警这个场景，告警就是一个事件。当该 事件发生时，SkyWalking就会主动去调用一个配置好的接口，该接口就是所谓的Webhook。 </p>
<p>SkyWalking的告警消息会通过 HTTP 请求进行发送，请求方法为 POST，Content-Type 为 application/json，其JSON 数据实基于List&lt; org.apache.skywalking.oap.server.core.alarm.AlarmMessage &gt;进行序列化。JSON数据实例如下：</p>
<pre><code class="json">[&#123;
    &quot;scopeId&quot;: 1, 
    &quot;scope&quot;: &quot;SERVICE&quot;,
    &quot;name&quot;: &quot;serviceA&quot;, 
    &quot;id0&quot;: &quot;12&quot;,  
    &quot;id1&quot;: &quot;&quot;,  
    &quot;ruleName&quot;: &quot;service_resp_time_rule&quot;,
    &quot;alarmMessage&quot;: &quot;alarmMessage xxxx&quot;,
    &quot;startTime&quot;: 1560524171000
&#125;, &#123;
    &quot;scopeId&quot;: 1,
    &quot;scope&quot;: &quot;SERVICE&quot;,
    &quot;name&quot;: &quot;serviceB&quot;,
    &quot;id0&quot;: &quot;23&quot;,
    &quot;id1&quot;: &quot;&quot;,
    &quot;ruleName&quot;: &quot;service_resp_time_rule&quot;,
    &quot;alarmMessage&quot;: &quot;alarmMessage yyy&quot;,
    &quot;startTime&quot;: 1560524171000
&#125;]
</code></pre>
<p>字段说明：</p>
<ul>
<li><strong>scopeId、scope</strong>：所有可用的 Scope 详见 org.apache.skywalking.oap.server.core.source.DefaultScopeDefine</li>
<li><strong>name</strong>：目标 Scope 的实体名称</li>
<li><strong>id0</strong>：Scope 实体的 ID</li>
<li><strong>id1</strong>：保留字段，目前暂未使用</li>
<li><strong>ruleName</strong>：告警规则名称</li>
<li><strong>alarmMessage</strong>：告警消息内容</li>
<li><strong>startTime</strong>：告警时间，格式为时间戳 </li>
</ul>
<h5 id="告警实现发送邮件"><a href="#告警实现发送邮件" class="headerlink" title="告警实现发送邮件"></a>告警实现发送邮件</h5><ol>
<li><p>导入依赖</p>
<pre><code class="pom">&lt;dependency&gt;
    &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
    &lt;artifactId&gt;spring-boot-starter-mail&lt;/artifactId&gt;
&lt;/dependency&gt;
</code></pre>
</li>
<li><p>添加发送邮件配置</p>
<pre><code class="yml">spring:
  # 邮箱配置
  mail:
    host: smtp.163.com
    #发送者邮箱账号
    username: kiwi_mh@163.com
    #发送者密钥 QWFMJBVPAKJZLPNR   POP3
    password: QWFMJBVPAKJZLPNR
    default‐encoding: UTF-8
    port: 465 #端口号465或587
    protocol: smtps
    properties:
     mail:
     debug: false
     smtp:
     socketFactory:
      class: javax.net.ssl.SSLSocketFactory
</code></pre>
</li>
<li><p>提供告警接口</p>
<pre><code class="java">@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping(&quot;/alarm&quot;)
public class SwAlarmController &#123;

    private final JavaMailSender sender;

    @Value(&quot;$&#123;spring.mail.username&#125;&quot;)
    private String from;


    /**
     * 接收skywalking服务的告警通知并发送至邮箱
     *
     * 必须是post
     */
    @PostMapping(&quot;/receive&quot;)
    public void receive(@RequestBody List&lt;SwAlarmDto&gt; alarmList) &#123;
        SimpleMailMessage message = new SimpleMailMessage();
        // 发送者邮箱
        message.setFrom(from);
        // 接收者邮箱
        message.setTo(from);
        // 主题
        message.setSubject(&quot;告警邮件&quot;);
        String content = getContent(alarmList);
        // 邮件内容
        message.setText(content);
        sender.send(message);
        log.info(&quot;告警邮件已发送...&quot;+content);
    &#125;

    private String getContent(List&lt;SwAlarmDto&gt; alarmList) &#123;
        StringBuilder sb = new StringBuilder();
        for (SwAlarmDto dto : alarmList) &#123;
            sb.append(&quot;scopeId: &quot;).append(dto.getScopeId())
                    .append(&quot;\nscope: &quot;).append(dto.getScope())
                    .append(&quot;\n目标 Scope 的实体名称: &quot;).append(dto.getName())
                    .append(&quot;\nScope 实体的 ID: &quot;).append(dto.getId0())
                    .append(&quot;\nid1: &quot;).append(dto.getId1())
                    .append(&quot;\n告警规则名称: &quot;).append(dto.getRuleName())
                    .append(&quot;\n告警消息内容: &quot;).append(dto.getAlarmMessage())
                    .append(&quot;\n告警时间: &quot;).append(dto.getStartTime())
                    .append(&quot;\n标签: &quot;).append(dto.getTags())
                    .append(&quot;\n\n---------------\n\n&quot;);
        &#125;

        return sb.toString();
    &#125;

&#125;
</code></pre>
<p>DTO</p>
<pre><code class="java">@Data
public class SwAlarmDto &#123;

    private int scopeId;
    private String scope;
    private String name;
    private String id0;
    private String id1;
    private String ruleName;
    private String alarmMessage;
    private List&lt;Tag&gt; tags;
    private long startTime;
    private transient int period;
    private transient boolean onlyAsCondition;


    @Data
    public static class Tag&#123;
        private String key;
        private String value;
    &#125;

&#125;
</code></pre>
</li>
<li><p>添加Webhook调用的接口<br>修改apache-skywalking-apm-bin\config\alarm-settings.yml中的webhooks配置。接口为上一步编写的接口。</p>
<pre><code class="yml">webhooks:
  - http://127.0.0.1:8088/alarm/receive
</code></pre>
</li>
</ol>
<p>启动项目和Skywalking服务，在任意一个接口中睡眠两秒，请求几次该接口。就可以收到告警的短信了。</p>
<p>我这边在all接口中睡眠了2秒，调用all接口几次，触发告警。收到邮件。</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673504613732.png" alt="1673504613732"></p>
<p>告警邮件</p>
<p><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673504664305.png" alt="1673504664305"></p>
<h3 id="SkyWalking集群"><a href="#SkyWalking集群" class="headerlink" title="SkyWalking集群"></a>SkyWalking集群</h3><p>SkyWalking OAP一般都是以集群的方式出现的。那么实现SkyWalking集群是将SkyWalking OAP作为一个服务注册到nacos上，只要SkyWalking OAP服务没有全部宕机，保证有一个SkyWalking OAP在运行，就能进行跟踪。</p>
<ul>
<li><p>修改cluster方式<br>修改apache-skywalking-apm-bin\config\application.yml文件中的cluster方式为nacos。<br><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673505324721.png" alt="1673505324721"></p>
</li>
<li><p>配置SkyWalking UI服务访问的地址<br>修改apache-skywalking-apm-bin\webapp\webapp.yml文件，在oap-service下添加oap服务列表。<br><img src="https://gitee.com/xiaomaer-sss/picgo/raw/master/images/2023/1673505512692.png" alt="1673505512692"></p>
</li>
<li><p>启动服务<br>启动时和单机的相同，需要将所有的OAP服务加入到参数中，多个使用英文逗号隔开。<br>startup.sh</p>
<pre><code class="shell"> #!/bin/sh
 # SkyWalking Agent配置
export SW_AGENT_NAME=springboot‐skywalking‐demo
export SW_AGENT_COLLECTOR_BACKEND_SERVICES=192.168.2.7:11800,192.168.2.8:11800,192.168.2.9:11800 # 多个使用英文逗号隔开
export SW_AGENT_SPAN_LIMIT=2000
export JAVA_AGENT=‐javaagent:/usr/local/soft/apache‐skywalking‐apm‐bin/agent/skywalking‐agent.jar
java $JAVA_AGENT ‐jar springboot‐skywalking‐demo‐0.0.1‐SNAPSHOT.jar
</code></pre>
</li>
</ul>

      
       <hr><span style="font-style: italic;color: gray;"> 转载请注明来源，欢迎对文章中的引用来源进行考证，欢迎指出任何有错误或不够清晰的表达。可以邮件至 jackma.h@foxmail.com </span>
    </div>
</article>


<p>
    <a  class="dashang" onclick="dashangToggle()">💰</a>
</p>






    




    </div>
    <div class="copyright">
        <p class="footer-entry">
    ©2017 kiwi
</p>
<p class="footer-entry">Built with <a href="https://hexo.io/" target="_blank">Hexo</a> and <a href="https://github.com/yelog/hexo-theme-3-hexo" target="_blank">3-hexo</a> theme</p>

    </div>
    <div class="full-toc">
        <button class="full" data-title="切换全屏 快捷键 s"><span class="min "></span></button>
<a class="" id="rocket" ></a>

    </div>
</div>

<div class="hide_box" onclick="dashangToggle()"></div>
<div class="shang_box">
    <a class="shang_close"  onclick="dashangToggle()">×</a>
    <div class="shang_tit">
        <p>Help us with donation</p>
    </div>
    <div class="shang_payimg">
        <div class="pay_img">
            <img src="/img/alipay.jpg" class="alipay" title="扫码支持">
            <img src="/img/weixin.jpg" class="weixin" title="扫码支持">
        </div>
    </div>
    <div class="shang_payselect">
        <span><label><input type="radio" name="pay" checked value="alipay">alipay</label></span><span><label><input type="radio" name="pay" value="weixin">weixin</label></span>
    </div>
</div>


</body>
<script src="/js/jquery.pjax.js?v=1.1.0" ></script>

<script src="/js/script.js?v=1.1.0" ></script>
<script>
    var img_resize = 'default';
    function initArticle() {
        /*渲染对应的表格样式*/
        
            $("#post .pjax table").addClass("green_title");
        

        /*渲染打赏样式*/
        
        $("input[name=pay]").on("click", function () {
            if($("input[name=pay]:checked").val()=="weixin"){
                $(".shang_box .shang_payimg .pay_img").addClass("weixin_img");
            } else {
                $(".shang_box .shang_payimg .pay_img").removeClass("weixin_img");
            }
        })
        

        /*高亮代码块行号*/
        

        /*访问数量*/
        
        $.getScript("//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js");
        

        /*代码高亮，行号对齐*/
        $('.pre-numbering').css('line-height',$('.has-numbering').css('line-height'));

        
        
    }

    /*打赏页面隐藏与展示*/
    
    function dashangToggle() {
        $(".shang_box").fadeToggle();
        $(".hide_box").fadeToggle();
    }
    

</script>

<!--加入行号的高亮代码块样式-->

<!--自定义样式设置-->
<style>
    
    
    .nav {
        width: 542px;
    }
    .nav.fullscreen {
        margin-left: -542px;
    }
    .nav-left {
        width: 120px;
    }
    
    
    @media screen and (max-width: 1468px) {
        .nav {
            width: 492px;
        }
        .nav.fullscreen {
            margin-left: -492px;
        }
        .nav-left {
            width: 100px;
        }
    }
    
    
    @media screen and (max-width: 1024px) {
        .nav {
            width: 492px;
            margin-left: -492px
        }
        .nav.fullscreen {
            margin-left: 0;
        }
    }
    
    @media screen and (max-width: 426px) {
        .nav {
            width: 100%;
        }
        .nav-left {
            width: 100%;
        }
    }
    
    
    .nav-right .title-list nav a .post-title, .nav-right .title-list #local-search-result a .post-title {
        color: #383636;
    }
    
    
    .nav-right .title-list nav a .post-date, .nav-right .title-list #local-search-result a .post-date {
        color: #5e5e5f;
    }
    
    
    .nav-right nav a.hover, #local-search-result a.hover{
        background-color: #e2e0e0;
    }
    
    

    /*列表样式*/
    
    #post .pjax article .article-entry>ol, #post .pjax article .article-entry>ul, #post .pjax article>ol, #post .pjax article>ul{
        border: #e2dede solid 1px;
        border-radius: 10px;
        padding: 10px 32px 10px 56px;
    }
    #post .pjax article .article-entry li>ol, #post .pjax article .article-entry li>ul,#post .pjax article li>ol, #post .pjax article li>ul{
        padding-top: 5px;
        padding-bottom: 5px;
    }
    #post .pjax article .article-entry>ol>li, #post .pjax article .article-entry>ul>li,#post .pjax article>ol>li, #post .pjax article>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    #post .pjax article .article-entry li>ol>li, #post .pjax article .article-entry li>ul>li,#post .pjax article li>ol>li, #post .pjax article li>ul>li{
        margin-bottom: auto;
        margin-left: auto;
    }
    

    /* 背景图样式 */
    
    #post {
        background: url([object Object]);
    }
    
    
    #post .index {
        background: url([object Object]);
    }
    


    /*引用块样式*/
    

    /*文章列表背景图*/
    
    .nav-right:before {
        content: ' ';
        display: block;
        position: absolute;
        left: 0;
        top: 0;
        width: 100%;
        height: 100%;
        opacity: 0.3;
        background: url("https://i.loli.net/2019/07/22/5d3521411f3f169375.png");
        background-repeat: no-repeat;
        background-position: 50% 0;
        -ms-background-size: cover;
        -o-background-size: cover;
        -moz-background-size: cover;
        -webkit-background-size: cover;
        background-size: cover;
    }
    

    
</style>







</html>
